<template>
	<u-popup v-model="value" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true"
	 close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close">
		<u-tabs v-if="value" :list="genTabsList" :is-scroll="true" :current="tabsIndex" @change="tabsChange" ref="tabs"></u-tabs>
		<view class="area-box">
			<view class="u-flex" :class="{ 'change':isChange }">
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group>
								<u-cell-item v-for="(item,index) in countrys" :title="item.name" :arrow="false" :index="index" :key="index"
								 @click="countryChange">
									<u-icon v-if="isChooseG&&country===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group>
								<u-cell-item v-for="(item,index) in provinces" :title="item.name" :arrow="false" :index="index" :key="index"
								 @click="provinceChange">
									<u-icon v-if="isChooseP&&province===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseP">
								<u-cell-item v-for="(item,index) in citys" :title="item.name" :arrow="false" :index="index" :key="index"
								 @click="cityChange">
									<u-icon v-if="isChooseC&&city===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>

				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseC">
								<u-cell-item v-for="(item,index) in areas" :title="item.name" :arrow="false" :index="index" :key="index"
								 @click="areaChange">
									<u-icon v-if="isChooseA&&area===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
				
				<view class="area-item">
					<view class="u-padding-10 u-bg-gray" style="height: 100%;">
						<scroll-view :scroll-y="true" style="height: 100%">
							<u-cell-group v-if="isChooseA">
								<u-cell-item v-for="(item,index) in villages" :title="item.name" :arrow="false" :index="index" :key="index"
								 @click="villageChange">
									<u-icon v-if="isChooseV&&village===index" slot="right-icon" size="34" name="checkbox-mark"></u-icon>
								</u-cell-item>
							</u-cell-group>
						</scroll-view>
					</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	import {getParAreaList,getAreaTrees} from '@/api/index.js'
	export default {
		name: 'u-city-select',
		props: {
			// 通过双向绑定控制组件的弹出与收起
			value: {
				type: Boolean,
				default: false
			},
			// 默认显示的地区，可传类似["河北省", "秦皇岛市", "北戴河区"]
			defaultRegion: {
				type: Array,
				default () {
					return [];
				}
			},
			// 默认显示地区的编码，defaultRegion和areaCode同时存在，areaCode优先，可传类似["13", "1303", "130304"]
			areaCode: {
				type: Array,
				default () {
					return [];
				}
			},
			// 是否允许通过点击遮罩关闭Picker
			maskCloseAble: {
				type: Boolean,
				default: true
			},
			// 弹出的z-index值
			zIndex: {
				type: [String, Number],
				default: 0
			}
		},
		data() {
			return {
				cityValue: "",
				countrys:[],
				country:0,//国家下标
				isChooseG:false, //是否已经选择了国家
				isChooseP: false, //是否已经选择了省
				province: 0, //省级下标
				provinces: [],
				isChooseC: false, //是否已经选择了市
				city: 0, //市级下标
				citys: [],
				isChooseA: false, //是否已经选择了区
				area: 0, //区级下标
				areas: [],
				villages:[],
				village:0,//小区下标
				isChooseV:false, //是否已经选择了小区
				tabsIndex: 0,
			}
		},
		mounted() {
			this.init();
		},
		computed: {
			isChange() {
				return this.tabsIndex > 1;
			},
			genTabsList() {
				let tabsList = [{
					name: "请选择"
				}];
				if (this.isChooseG) {
					tabsList[0]['name'] = this.countrys[this.country]['name'];
					tabsList[1] = {
						name: "请选择"
					};
				}
				if (this.isChooseP) {
					tabsList[1]['name'] = this.provinces[this.province]['name'];
					tabsList[2] = {
						name: "请选择"
					};
				}
				if (this.isChooseC) {
					tabsList[2]['name'] = this.citys[this.city]['name'];
					tabsList[3] = {
						name: "请选择"
					};
				}
				if (this.isChooseA) {
					tabsList[3]['name'] = this.citys[this.city]['name'];
					tabsList[4] = {
						name: "请选择"
					};
				}
				if (this.isChooseV) {
					tabsList[4]['name'] = this.areas[this.area]['name'];
				}
				return tabsList;
			},
			uZIndex() {
				// 如果用户有传递z-index值，优先使用
				return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
			}
		},
		methods: {
			init() {
				getParAreaList().then(res=>{
					this.countrys=res
				})
			},
			async getAreaTrees(id){
				const res = await getAreaTrees({parentId:id})
				return res
			},
			// setProvince(label = "", value = "") {
			// 	this.provinces.map((v, k) => {
			// 		if (value ? v.value == value : v.label == label) {
			// 			this.provinceChange(k);
			// 		}
			// 	})
			// },
			// setCity(label = "", value = "") {
			// 	this.citys.map((v, k) => {
			// 		if (value ? v.value == value : v.label == label) {
			// 			this.cityChange(k);
			// 		}
			// 	})
			// },
			// setArea(label = "", value = "") {
			// 	this.areas.map((v, k) => {
			// 		if (value ? v.value == value : v.label == label) {
			// 			this.isChooseA = true;
			// 			this.area = k;
			// 		}
			// 	})
			// },
			close() {
				this.$emit('input', false);
			},
			tabsChange(index) {
				this.tabsIndex = index;
			},
			async countryChange(index){
				this.provinces = await this.getAreaTrees(this.countrys[index].id)
				this.isChooseG = true;
				this.isChooseP = false;
				this.isChooseC = false;
				this.isChooseA = false;
				this.isChooseV = false;
				this.country = index;
				this.tabsIndex = 1;
			},
			async provinceChange(index) {
				this.citys = await this.getAreaTrees(this.provinces[index].id)
				this.isChooseP = true;
				this.isChooseC = false;
				this.isChooseA = false;
				this.isChooseV = false;
				this.province = index;
				this.tabsIndex = 2;
			},
			async cityChange(index) {
				this.areas = await this.getAreaTrees(this.citys[index].id)
				this.isChooseC = true;
				this.isChooseA = false;
				this.isChooseV = false;
				this.city = index;
				this.tabsIndex = 3;
			},
			async areaChange(index) {
				this.villages = await this.getAreaTrees(this.areas[index].id)
				this.isChooseA = true;
				this.isChooseV = false;
				this.area = index;
				this.tabsIndex = 4;
			},
			villageChange(index) {
				this.isChooseV = true;
				this.village = index;
				const data={
					name:this.villages[index].name,
					id:this.villages[index].id
				}
				this.$emit('city-change', data);
				this.close();
			}
		}

	}
</script>
<style lang="scss">
	.area-box {
		width: 100%;
		overflow: hidden;
		height: 800rpx;

		>view {
			width: 150%;
			transition: transform 0.3s ease-in-out 0s;
			transform: translateX(0);

			&.change {
				transform: translateX(-33.3333333%);
			}
		}

		.area-item {
			width: 33.3333333%;
			height: 800rpx;
		}
	}
</style>
